package com.boxedinsoftware.ePost;

import java.io.IOException;


import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class getLogin extends Activity {
	
	protected EditText login;
	protected EditText password;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        
        //Assign Views to variables
        login = (EditText)findViewById(R.id.EditText01);
        password = (EditText)findViewById(R.id.EditText02); 
        Button logIn = (Button)findViewById(R.id.Button01);
        
        //Action Listener for "Log In" Button
        logIn.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				
				if(login.getText().toString().length() == 0)
					Toast.makeText(getLogin.this.getApplicationContext(), "Please enter a username", Toast.LENGTH_LONG).show();
				else if(password.getText().toString().length() == 0)
					Toast.makeText(getLogin.this.getApplicationContext(), "Please enter a password", Toast.LENGTH_LONG).show();
				else{
					TextView text = (TextView)findViewById(R.id.TextView03);
			        
			        
			        text.setText("Trying to log in");
					new getContents().execute("");
				}	
			}
        	
        	
        	
        });
        
        
        
    }
    
    public void launch()
    {
    	finish();
    	startActivity(new Intent("com.boxedinsoftware.ePost.mainList"));
		
    	
    }
    
    
 public class getContents extends AsyncTask<String, Void, String> {
	 final ProgressDialog progress = ProgressDialog.show(getLogin.this,    
             "Please Wait ", "Requesting login credentials... ", true);
    	
    	@Override
    	protected String doInBackground(String... params) {
				   
    		try {
    		    
    		    //Assigning base URL ( no HTTPS now , port , user-name and password to the Basic HTTP Auth 
    		    CredentialsProvider credProvider = new BasicCredentialsProvider();
    		    credProvider.setCredentials(new AuthScope("www.cse.yorku.ca", 443),
    		        new UsernamePasswordCredentials(login.getText().toString(), password.getText().toString()));
    		    
    		    //Initializing  HttpClient and assigning credentials to it
    		    DefaultHttpClient http = new DefaultHttpClient();
    		    http.setCredentialsProvider(credProvider);
    		    
    		    //Assigning the HTTPS login URL
    		    HttpPut loginURL = new HttpPut("https://www.cse.yorku.ca/~roumani/ePost/server/ep.cgi");
    		    
    		    //Requesting response
    		    HttpResponse response = http.execute(loginURL);
    		    
    		    //returning HTTP Header response either 200 - OK or 401 - Invalid user-name or password
    		    return response.getStatusLine().getStatusCode() + "";
    		    
    		} catch (ClientProtocolException e) {
    		    //
    			return e.getMessage();
    		} catch (IOException e) {
    			//
    		    return e.getMessage();
    		}
 
	       
    	}

    	/* (non-Javadoc)
    	 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
    	 */
    	@Override
    	protected void onPostExecute(String result) {
    		
    		TextView status = (TextView)findViewById(R.id.TextView03);
    		
    		if(result.equalsIgnoreCase("200")){
    			status.setText("Login Successful.");
    			launch();
    			progress.dismiss();
    		}
    		else if(result.equalsIgnoreCase("401")){
    			status.setText("Invalid Username or Password");
    			progress.dismiss();
    		}
    		else{
    			status.setText(result);
    			progress.dismiss();
    		}
    		super.onPostExecute(result);
    	}
    	/* (non-Javadoc)
    	 * @see android.os.AsyncTask#onPreExecute()
    	 */
    	@Override
    	protected void onPreExecute() {
    		
    		super.onPreExecute();
    	}

    	/* (non-Javadoc)
    	 * @see android.os.AsyncTask#onProgressUpdate(Progress[])
    	 */
    	@Override
    	protected void onProgressUpdate(Void... values) {
    		// TODO Auto-generated method stub
    		super.onProgressUpdate(values);
    	}

    }

    
}